# Make the coefplot for anyDA design

library(here)

load(here("Analysis", "analysis_anyDA_new.rda"))

y_axis_labels <- row.names(res[1:2, ])
y_axis_labels <- c("Social\nCohesion", "Collective\nEfficacy")

pdf(file = here("Figures_Tables", "coefplot_anyDA_new.pdf"), width = 8, height = 3, pointsize = 16)
par(oma = rep(0, 4), mar = c(4, 1, 2, 0), mgp = c(3, .5, 0), xpd = NA)
oldmai <- par("mai")
newmai <- oldmai
newmai[2] <- max(strwidth(y_axis_labels, units = "inches")) + .2
par(mai = newmai)
nmods <- nrow(res[1:2, ])
yloc <- (1:nmods) - 0.5
theylim <- range(as.vector(res[1:2, ])[is.finite(as.vector(res[1:2, ]))])
plot(theylim, c(0.5, nmods - .5),
  xlim = range(c(0, theylim)),
  type = "n", axes = FALSE, ylab = "",
  xlab = "Effect of 1 Unit Difference in Perceptions of Visible Minorities \n in Local Community (+/- 95% CI)"
)
segments(res[1:2, "ci1"], yloc, res[1:2, "ci2"], yloc, lwd = 1.5)
points(res[1:2, "Estimate"], yloc, pch = 19)
# abline(v = 0, lwd = .5, col = gray(.7))
axis(1, line = .5)
axis(2, at = yloc, labels = y_axis_labels, las = 1) # , pos = theylim[1] + 0.01)
par(mai = oldmai)
dev.off()

system("touch Figures_Tables/figures_anyDA.done")
